package com.mdhw.utils;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;

/**
 * @author: amoyiki
 * @date: 2022/3/21
 */
public class MoneyUtil {

    public static String toPlainString(BigDecimal bd) {
        if (bd == null) {
            return null;
        }
        return bd.stripTrailingZeros().toPlainString();
    }

    public static BigDecimal stripZeros(BigDecimal bd) {
        if (bd == null) {
            return null;
        }
        return new BigDecimal(bd.stripTrailingZeros().toPlainString());
    }

    /**
     * 相乘结果值保留小数点，默认两位
     * @param bd1
     * @param bd2
     * @param round
     * @return
     */
    public static BigDecimal multiply(BigDecimal bd1, BigDecimal bd2, Integer round) {
        if (round == null) {
            round = 2;
        }
        if (bd1 == null) {
            return bd2.setScale(round, BigDecimal.ROUND_HALF_UP);
        }
        if (bd2 == null) {
            return bd1.setScale(round, BigDecimal.ROUND_HALF_UP);
        }
        BigDecimal bd3 = bd1.multiply(bd2).setScale(round, BigDecimal.ROUND_HALF_UP);
        return bd3;
    }
    /**
     * 相乘结果值保留小数点，默认两位
     * @param bd1
     * @param bd2
     * @param round
     * @return
     */
    public static BigDecimal multiply(String bd1, String bd2, Integer round) {
        if (round == null) {
            round = 2;
        }
        if (!StringUtils.hasText(bd1)) {
            return new BigDecimal(bd2).setScale(round, BigDecimal.ROUND_HALF_UP);
        }
        if (!StringUtils.hasText(bd2)) {
            return new BigDecimal(bd1).setScale(round, BigDecimal.ROUND_HALF_UP);
        }
        BigDecimal bd3 = new BigDecimal(bd1).multiply(new BigDecimal(bd2)).setScale(round, BigDecimal.ROUND_HALF_UP);
        return bd3;
    }
    /**
     * 相减结果值保留小数点，默认两位
     * @param bd1
     * @param bd2
     * @param round
     * @return
     */
    public static BigDecimal subtract(BigDecimal bd1, BigDecimal bd2, Integer round) {
        if (round == null) {
            round = 2;
        }
        if (bd1 == null) {
            return null;
        }
        if (bd2 == null) {
            return bd1.setScale(round, BigDecimal.ROUND_HALF_UP);
        }
        BigDecimal bd3 = bd1.subtract(bd2).setScale(round, BigDecimal.ROUND_HALF_UP);
        return bd3;
    }
    /**
     * 相减结果值保留小数点，默认两位
     * @param bd1
     * @param bd2
     * @param round
     * @return
     */
    public static BigDecimal subtract(String bd1, String bd2, Integer round) {
        if (round == null) {
            round = 2;
        }
        if (!StringUtils.hasText(bd1)) {
            return null;
        }
        if (!StringUtils.hasText(bd2)) {
            return new BigDecimal(bd1).setScale(round, BigDecimal.ROUND_HALF_UP);
        }
        BigDecimal bd3 = new BigDecimal(bd1).subtract(new BigDecimal(bd2)).setScale(round, BigDecimal.ROUND_HALF_UP);
        return bd3;
    }

    /**
     * 相除结果值保留小数点，默认两位
     * @param bd1
     * @param bd2
     * @param round
     * @return
     */
    public static BigDecimal divide(BigDecimal bd1, BigDecimal bd2, Integer round) {
        if (round == null) {
            round = 2;
        }
        if (bd1 == null || bd2 == null) {
            return null;
        }
        BigDecimal bd3 = bd1.divide(bd2,round).setScale(round, BigDecimal.ROUND_HALF_UP);
        return bd3;
    }
    /**
     * 相除结果值保留小数点，默认两位
     * @param bd1
     * @param bd2
     * @param round
     * @return
     */
    public static BigDecimal divide(String bd1, String bd2, Integer round) {
        if (round == null) {
            round = 2;
        }
        if (!StringUtils.hasText(bd1)) {
            return null;
        }
        if (!StringUtils.hasText(bd2)) {
            return new BigDecimal(bd1).setScale(round, BigDecimal.ROUND_HALF_UP);
        }
        BigDecimal bd3 = new BigDecimal(bd1).divide(new BigDecimal(bd2)).setScale(round, BigDecimal.ROUND_HALF_UP);
        return bd3;
    }
}
